package com.nulldev.util.web.nTube.pages;

import com.nulldev.util.languages.json.uniapi.UniJsonValue;
import com.nulldev.util.manage.nullUtil;
import com.nulldev.util.networking.ip.connstatus.NetworkStatusTracker;
import com.nulldev.util.osAPI.OS;
import com.nulldev.util.web.HttpServer3.Response;
import com.nulldev.util.web.HttpServer3.pages.v3.HttpPage3;
import com.nulldev.util.web.nTube.NTubeServer;
import com.nulldev.util.web.nTube.util.INTubeInstance;

public class AboutAPIPage extends HttpPage3 {

	private final INTubeInstance nts;

	public AboutAPIPage(final INTubeInstance nts) {
		super("/api/about");
		this.setCachingMethod(CachingMethod.VERY_SHORT_CACHE);
		this.setUseJSONCaptchaPage(true);
		this.setChecks(EnhancedSecurityChecks.DISALLOW_POST_DATA, EnhancedSecurityChecks.ONLY_ALLOW_BASIC_METHODS);
		this.nts = nts;
	}

	@SuppressWarnings("resource")
	@Override
	public Response onRequest(Request request) {
		final UniJsonValue v = UniJsonValue.object();
		v.add("api_version", 2.0);
		v.add("video_api_version", GetVideoAsyncPage.VIDEO_API_STABLE_VER);
		v.add("video_api_version_ns", GetVideoAsyncPage.VIDEO_API_STABLE_VER_NS.asVersionString());
		final UniJsonValue ntube = UniJsonValue.object();
		ntube.add("version", NTubeServer.nTube_RELEASE_STRING);
		ntube.add("isLTS", NTubeServer.nTube_RELEASE_IS_LTS);
		final UniJsonValue nU = UniJsonValue.object();
		nU.add("version", nullUtil.VERSION);
		nU.add("release", nullUtil.getVersionString());
		v.add("nullUtil", nU);
		v.add("nTube", ntube);
		final UniJsonValue inst = UniJsonValue.object();
		inst.add("platform", OS.get().toString());
		if (OS.isWindows())
			inst.add("platformVersion", OS.Windows_Type.getType().toString());
		inst.add("online", NetworkStatusTracker.isConnected());
		inst.add("outdatedInstance", false);
		v.add("instance", inst);
		if (this.nts.hasInstanceInfo()) {
			v.add("instanceInfo", this.nts.getInstanceInfo().asObject());
		}
		return new Response(200, v, v).setCustomMime("application/json");
	}
}
